/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package facades.persistence;

import facades.persistence.exceptions.NonexistentEntityException;
import facades.persistence.exceptions.RollbackFailureException;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import javax.transaction.UserTransaction;

/**
 *
 * @author Alvaro
 */
public class MovementTransferJpaController implements Serializable {

    public MovementTransferJpaController(UserTransaction utx, EntityManagerFactory emf) {
        this.utx = utx;
        this.emf = emf;
    }
    private UserTransaction utx = null;
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(MovementTransfer movementTransfer) throws RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            User userIduser = movementTransfer.getUserIduser();
            if (userIduser != null) {
                userIduser = em.getReference(userIduser.getClass(), userIduser.getIduser());
                movementTransfer.setUserIduser(userIduser);
            }
            em.persist(movementTransfer);
            if (userIduser != null) {
                userIduser.getMovementTransferList().add(movementTransfer);
                userIduser = em.merge(userIduser);
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(MovementTransfer movementTransfer) throws NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            MovementTransfer persistentMovementTransfer = em.find(MovementTransfer.class, movementTransfer.getIdmovementTransfer());
            User userIduserOld = persistentMovementTransfer.getUserIduser();
            User userIduserNew = movementTransfer.getUserIduser();
            if (userIduserNew != null) {
                userIduserNew = em.getReference(userIduserNew.getClass(), userIduserNew.getIduser());
                movementTransfer.setUserIduser(userIduserNew);
            }
            movementTransfer = em.merge(movementTransfer);
            if (userIduserOld != null && !userIduserOld.equals(userIduserNew)) {
                userIduserOld.getMovementTransferList().remove(movementTransfer);
                userIduserOld = em.merge(userIduserOld);
            }
            if (userIduserNew != null && !userIduserNew.equals(userIduserOld)) {
                userIduserNew.getMovementTransferList().add(movementTransfer);
                userIduserNew = em.merge(userIduserNew);
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = movementTransfer.getIdmovementTransfer();
                if (findMovementTransfer(id) == null) {
                    throw new NonexistentEntityException("The movementTransfer with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            MovementTransfer movementTransfer;
            try {
                movementTransfer = em.getReference(MovementTransfer.class, id);
                movementTransfer.getIdmovementTransfer();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The movementTransfer with id " + id + " no longer exists.", enfe);
            }
            User userIduser = movementTransfer.getUserIduser();
            if (userIduser != null) {
                userIduser.getMovementTransferList().remove(movementTransfer);
                userIduser = em.merge(userIduser);
            }
            em.remove(movementTransfer);
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<MovementTransfer> findMovementTransferEntities() {
        return findMovementTransferEntities(true, -1, -1);
    }

    public List<MovementTransfer> findMovementTransferEntities(int maxResults, int firstResult) {
        return findMovementTransferEntities(false, maxResults, firstResult);
    }

    private List<MovementTransfer> findMovementTransferEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(MovementTransfer.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public MovementTransfer findMovementTransfer(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(MovementTransfer.class, id);
        } finally {
            em.close();
        }
    }

    public int getMovementTransferCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<MovementTransfer> rt = cq.from(MovementTransfer.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
